package edu.msu.scrabble.project1;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class CreateUserActivity extends Activity {
	
	private final static String USERNAME = "username";
	private final static String PASSWORD = "password";
	private final static String VERIFYPASSWORD = "verifyPassword";
	
	// for storing username and password on the device
	public static final String PREFERENCES = "preferences";
	
	private String username = null;
	
	private String password = null;
	
	private String verifyPassword = null;
	
	private EditText usernameEditText = null;
	
	private EditText passwordEditText = null;
	
	private EditText verifyPasswordEditText = null;
	
	private Cloud thecloud = null;
	
	//private SharedPreferences preferences = null;

	
	/**
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_user);
		
		// get the edittexts used for entering username and password
		usernameEditText = (EditText)findViewById(R.id.newUsernameText);
		passwordEditText = (EditText)findViewById(R.id.newPasswordText);
		verifyPasswordEditText = (EditText)findViewById(R.id.verifyNewPasswordText);
		
		//preferences = getSharedPreferences(PREFERENCES,0);
		
		// fill out information if there is a saved state
		if(savedInstanceState != null) {
			username = savedInstanceState.getString(USERNAME);
		    password = savedInstanceState.getString(PASSWORD);
		    verifyPassword = savedInstanceState.getString(VERIFYPASSWORD);
		    
		    usernameEditText.setText(username);
		    passwordEditText.setText(password);
		    verifyPasswordEditText.setText(verifyPassword);
		}
		
		thecloud = Cloud.get();
	}
	
	
	/**
	 * 
	 * @param view
	 */
	public void onActivate(View view) {
		username = usernameEditText.getText().toString();
        password = passwordEditText.getText().toString();
	    verifyPassword = verifyPasswordEditText.getText().toString();
	    
	    final ContextWrapper activity = this;
	    final Handler mainHandler = new Handler(this.getMainLooper());
	    
		// check that the password matches the verification
		if (!password.equals(verifyPassword))
		{
			// output a message saying passwords do not match
			Toast.makeText(activity, R.string.password_doesnt_match, Toast.LENGTH_SHORT).show();
			return;
		}
		
		/*
         * Create a thread to attempt to log in the user from the cloud
         */
        new Thread(new Runnable() {

            @Override
            public void run() {
            	if(thecloud.addUserToCloud(username, password)) {
            		// if successful login the new user -- We don't actually want to log in the user here, that's what the log in activity is for (Mike)
            		//if (thecloud.logInToCloud(username, password)) {
        				// go to the login screen, auto fill boxes
            			thecloud.setUsername(username);
            			Intent intent = new Intent(activity,LogInActivity.class);
            			intent.putExtra(USERNAME, username);
            			intent.putExtra(PASSWORD, password);
        				startActivity(intent);
        				finish();
        				
            		/*}
            		else {
            			mainHandler.post(new Runnable() {
                    		@Override
            	            public void run() {
		                		// output a message saying login was unsuccessful
		            			Toast.makeText(activity, R.string.login_failed, Toast.LENGTH_SHORT).show();
		            			
		                		// go to the login screen
		            			Intent intent = new Intent(activity,LogInActivity.class);
		            			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
		            			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        				startActivity(intent);
                    		}
                    	});
            		}*/
            	}
            	else {
            		mainHandler.post(new Runnable() {
                		@Override
        	            public void run() {
		            		// output a message saying creation of new user was unsuccessful
		            		Toast.makeText(activity, R.string.newuser_creation_failed, Toast.LENGTH_SHORT).show();
                		}
                	});
            	}
            }
        }).start();
	}
	
	
	/** 
     * Save the instance state
     */
    @Override
    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        
        username = usernameEditText.getText().toString();
        password = passwordEditText.getText().toString();
        verifyPassword = verifyPasswordEditText.getText().toString();
        
        bundle.putString(USERNAME, username);
        bundle.putString(PASSWORD, password);
        bundle.putString(VERIFYPASSWORD, verifyPassword);
    }

}
